package com.cxb.adapter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cxb.entity.GpsDestinationInfo;
import com.cxb.manager.HistoryManager;
import com.cxb.manager.NavigationManager;
import com.cxb.utils.Config;
import com.cxb.R;

public class HistoryAdapter extends BaseAdapter {
	private Context context; // 运行上下文
	private List<GpsDestinationInfo> listItems; // 导航历史记录集合
	private LayoutInflater listContainer; // 视图容器
	private NavigationManager navigationManager;
	private List<Integer> selected;
	private boolean editable = false;
	private List<GpsDestinationInfo> historyListChecked;

	public void setEditable(boolean editable) {
		this.editable = editable;
		this.notifyDataSetChanged();
	}

	public void setHistoryListChecked(
			List<GpsDestinationInfo> historyListChecked) {
		this.historyListChecked = historyListChecked;
	}

	public final class ListItemView { // 自定义控件集合
		public RelativeLayout rlyHistory;// 选择按钮
		public TextView tvName;// POI名称
		public ImageView cb_selected;
		public boolean checked = false;
	}

	public HistoryAdapter(Context context, List<GpsDestinationInfo> listItems) {
		this.context = context;
		listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
		this.listItems = listItems;
		navigationManager = new NavigationManager(context);
		selected = new LinkedList<Integer>();
	}

	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		if (listItems != null)
			return listItems.get(arg0);
		return null;
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final int selectID = position;
		// 自定义视图
		ListItemView listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		convertView = listContainer.inflate(R.layout.historylistitem, null);
		// 获取控件对象

		listItemView.cb_selected = (ImageView) convertView
				.findViewById(R.id.cb_selected);
		if (editable)
			listItemView.cb_selected.setVisibility(View.VISIBLE);
		else
			listItemView.cb_selected.setVisibility(View.GONE);

		listItemView.cb_selected.setImageResource(isChecked(listItems
				.get(selectID)) ? R.drawable.item_checkbox_checked
				: R.drawable.item_checkbox_uncheck);

		listItemView.tvName = (TextView) convertView.findViewById(R.id.tv_name);
		// 设置控件集到convertView
		convertView.setTag(listItemView);
		// 是否已经查看
		listItemView.tvName.setText((String) listItems.get(position).destName);
		return convertView;
	}

	public boolean isChecked(GpsDestinationInfo mGpsDestinationInfo) {
		if (historyListChecked == null)
			return false;
		for (int i = 0; i < historyListChecked.size(); i++) {
			if (historyListChecked.get(i).id == mGpsDestinationInfo.id) {
				return true;
			}
		}
		return false;
	}
}